﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuVien;
using System.Data;
using System.IO;


namespace TranTekBE.Marketing
{
    public partial class Add_Advertising : PagePopUp
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Load JS
            ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "LoadJS", "LoadJS();", true);
            if (!IsPostBack)
            {
                // Get đối tác cũ
                FunctionsUtils.GetDropListDoiTacQC(droplist_doitac_old);
                // Get vị trí quảng cáo
                FunctionsUtils.GetDropListViTriQC(droplist_VitriQC);
                // Get loại qc
                FunctionsUtils.GetDropListLoaiQC(droplist_LoaiQC);
                string get_IDQuangCao = FunctionsUtils.GetValueStringFromQueryString("idquangcao", "0");

                if (get_IDQuangCao != "0")
                {
                    FillDataEdit(get_IDQuangCao);
                }

                //
            }
            
            
        }

        bool CheckFileType(string fileName)
        {

            string ext = Path.GetExtension(fileName);

            switch (ext.ToLower())
            {

                case ".gif":

                    return true;

                case ".png":

                    return true;

                case ".jpg":

                    return true;

                case ".jpeg":

                    return true;

                default:

                    return false;

            }

        }
        protected void btnAddEvent_Click(object sender, EventArgs e)
        {
            string path_image = "";
            try
            {
                // Image
                if (FileUpload_Image.HasFile)
                {
                    if (CheckFileType(FileUpload_Image.FileName))
                    {
                        string date = DateTime.Now.ToString("yyMMddHHmmss");
                        string fileName = date + Path.GetFileName(FileUpload_Image.PostedFile.FileName);
                        path_image = "/images/Marketing/HinhQC/" + fileName;

                        FileUpload_Image.PostedFile.SaveAs(Server.MapPath(path_image));
                    }
                    else
                    {
                        ltrErr.Text = "File upload không đúng!";
                    }
                }
                // Insert
                if (droplist_VitriQC.SelectedIndex == 0 && droplist_LoaiQC.SelectedIndex == 0 && txtStartDate.Text == "" && txtEnddate.Text == "" && txtTienThang.Text == "")
                {
                    ltrErr.Text = "Dữ liệu còn trống, vui lòng kiểm tra lại";
                    ltrErr.ForeColor = System.Drawing.Color.Red;
                }
                else
                {
                    // Đối tác
                    string DoiTac = txtCty.Text.ToString().Replace("'", "''");
                    string DaiDien = txtDaiDien.Text.ToString().Replace("'", "''");
                    string DiaChi = txtDiaChi.Text.ToString().Replace("'", "''");
                    //
                    int DoiTacID = FunctionsUtils.ConvertToInt(droplist_doitac_old.SelectedValue.ToString(), 0);
                    // Quảng cáo
                    string vitriID = droplist_VitriQC.SelectedValue.ToString();
                    string loaiQCId = droplist_LoaiQC.SelectedValue.ToString();
                    string timeStart = txtStartDate.Text.ToString().Replace("'", "''");
                    string timeEnd = txtEnddate.Text.ToString().Replace("'", "''");
                    Int64 tien_thang = FunctionsUtils.ConvertToLong(txtTienThang.Text.ToString().Replace(",", ""), 0);

                    //
                    string url_image = path_image;
                    string sql = "";
                    //
                    string get_IDQuangCao = FunctionsUtils.GetValueStringFromQueryString("idquangcao", "0");

                    if (get_IDQuangCao != "0")
                    {
                        // Update
                        if (droplist_doitac_old.SelectedIndex != 0 && txtCty.Text == "" && txtDaiDien.Text == "" && txtDiaChi.Text == "")
                        {
                            sql = @"
                            UPDATE QuangCao
                               SET 
                                    DoiTacId = '" + DoiTacID + @"'
                                    ,StartDate = '" + timeStart + @"'
                                    ,EndDate = '" + timeEnd + @"'
                                    ,MoneyPerMonth = '" + tien_thang + @"'
                                    ,ViTriID = '" + vitriID + @"'
                                    ,UpdatedDate = '" + DateTime.Now.ToString() + @"'
                                    ,UpdatedBy = '" + UserInfo.UserID + @"'
                                    ,LoaiQuangCaoId = '" + loaiQCId + @"'
                                    ,HinhQuangCao = '" + url_image + @"'
                             WHERE QuangCaoId = '" + get_IDQuangCao + @"'
                            ";
                        }
                        else
                        {
                            sql = @"
                            UPDATE QuangCao
                               SET 
                                    DoiTacId = '" + DoiTacID + @"'
                                    ,StartDate = '" + timeStart + @"'
                                    ,EndDate = '" + timeEnd + @"'
                                    ,MoneyPerMonth = '" + tien_thang + @"'
                                    ,ViTriID = '" + vitriID + @"'
                                    ,UpdatedDate = '" + DateTime.Now.ToString() + @"'
                                    ,UpdatedBy = '" + UserInfo.UserID + @"'
                                    ,LoaiQuangCaoId = '" + loaiQCId + @"'
                                    ,HinhQuangCao = '" + url_image + @"'
                             WHERE QuangCaoId = '" + get_IDQuangCao + @"';

                            UPDATE DoiTac
                               SET 
                                    NguoiDaiDien = '" + DaiDien + @"'
                                    ,TenCongTy = '" + DoiTac + @"'
                                    ,[DiaChi] = '" + DiaChi + @"'
                             WHERE DoiTacId = '" + DoiTacID + @"'
                            ";
                        }
                    }
                    else
                    {
                        // Insert
                        if (droplist_doitac_old.SelectedIndex != 0 && txtCty.Text == "" && txtDaiDien.Text == "" && txtDiaChi.Text == "")
                        {
                            sql = @"
                                insert into QuangCao (DoiTacId,StartDate,EndDate,MoneyPerMonth,ViTriID,CreatedBy,UpdatedBy,LoaiQuangCaoId,HinhQuangCao) values(" + DoiTacID + ",'" + timeStart + "','" + timeEnd + "','" + tien_thang + "','" + vitriID + "','" + UserInfo.UserID + "','" + UserInfo.UserID + "','" + loaiQCId + "','" + url_image + @"')
                                ";

                        }
                        else
                        {
                            sql = @"
                                insert into DoiTac (NguoiDaiDien,TenCongTy,DiaChi) values(N'" + DaiDien + "',N'" + DoiTac + "',N'" + DiaChi + @"');
                                insert into QuangCao (DoiTacId,StartDate,EndDate,MoneyPerMonth,ViTriID,CreatedBy,UpdatedBy,LoaiQuangCaoId,HinhQuangCao) values(SCOPE_IDENTITY(),'" + timeStart + "','" + timeEnd + "','" + tien_thang + "','" + vitriID + "','" + UserInfo.UserID + "','" + UserInfo.UserID + "','" + loaiQCId + "','" + url_image + @"')
                                ";
                        }
                        
                    }
                    //
                    try
                    {

                        FunctionsUtils.Database2.ExecuteNonQuery(sql);

                    }
                    catch (Exception ex)
                    {
                        ltrErr.Text = ex.Message;
                        ltrErr.ForeColor = System.Drawing.Color.Red;
                    }
                    ClosePopupScripManager(true);
                }
            }
            catch (Exception err)
            {
                ltrErr.Text = err.Message;
                ltrErr.ForeColor = System.Drawing.Color.Red;
            }
        }
        public void FillDataEdit(string idQuangCao)
        {
            string sql_edit = "SELECT *  FROM QuangCao Where QuangCaoId = " + idQuangCao;
            DataSet ds = FunctionsUtils.Database2.ExecuteQuery(sql_edit);
            DataTable dt = ds.Tables[0];
            // Get data
            int DoiTacID = FunctionsUtils.ConvertToInt(dt.Rows[0]["DoiTacId"].ToString(), 0);
            int ViTriID = FunctionsUtils.ConvertToInt(dt.Rows[0]["ViTriID"].ToString(), 0);
            int LoaiQuangCaoId = FunctionsUtils.ConvertToInt(dt.Rows[0]["LoaiQuangCaoId"].ToString(), 0);
            string StartDate = dt.Rows[0]["StartDate"].ToString();
            string EndDate = dt.Rows[0]["EndDate"].ToString();
            string MoneyPerMonth = dt.Rows[0]["MoneyPerMonth"].ToString();
            // Fill data
            droplist_doitac_old.SelectedValue = DoiTacID.ToString();
            droplist_VitriQC.SelectedValue = ViTriID.ToString();
            droplist_LoaiQC.SelectedValue = LoaiQuangCaoId.ToString();
            txtStartDate.Text = StartDate;
            txtEnddate.Text = EndDate;
            txtTienThang.Text = MoneyPerMonth;
            
        }
    }
}